var base_url_materialGuide = '/rs/materialGuide';

var list = [];
var operation = "add";
var currentPageNo = 1;
var pageRows = 10;

$(function() {
    authName = getCookie('username');

    queryList();

    $("#btnUserQuery").click(function() {
        currentPageNo = 1;
        queryList();
    });
});

function queryList() {
    zhget(base_url_materialGuide, {
        page: currentPageNo,
        size: pageRows,
        sort: 'order_id'
    }, function(result) {
        list = result.rows;
        buildTable(result, 'materialGuide-template', 'materialGuide-placeholder');
    });
}

function sendpic() {
    var content = $("#picfile").val();
    if(content.length > 0) {
        var form = document.getElementById("picform");
        var formData = new FormData(form);
        upajax('/op/upload', formData, function (result) {
            result = JSON.parse(result);
            if(result.success != null){
                $("#picShow").attr("src", result.success);
                $("#picPath").val(result.success);
                $("#picfile").val('');
            }
            else if (result.error != null)
                alert(result.error);
        });
    }
    else
        alert('请您选择需要上传的图片！');
}

function onAddClick() {
    cleanForm();
    operation = "add";
    $('#userModal').modal('show');
}

function onUpdateClick(id) {
    fillForm(id);
    operation = "modify";
    $('#userModal').modal('show');
}

function onDeleteClick(id) {
    if(confirm("确认要删除？")) {
        zhdelete(base_url_materialGuide + "/" + id, {
            AUTHNAME:authName
        }, function (result) {
            if (result && result.affectedRows > 0) {
                queryList();
                showSuccess('删除成功！');
            } else {
                showError('删除失败！');
            }
        });
    }
}

function onSaveClick() {
    var id = $("#id").val();
    var data = {
        AUTHNAME:authName,
        url: $("#url").val(),
        subtitle: $("#subtitle").val(),
        order_id: $("#order_id").val(),
        picpath: $("#picPath").val()
    };
    if (operation == "add") {
        zhpost(base_url_materialGuide, data, saveResult);
    } else {
        zhput(base_url_materialGuide + "/" + id, data, saveResult);
    }
}

function saveResult(result) {
    if (result) {
        if (result.outcome==0) {
            showError('保存失败！');
        } else{
            $('#userModal').modal('hide');
            queryList();
            showSuccess('保存成功！');
        }
    } else {
        showError('保存失败！');
    }
}

function fillForm(id) {
    var index = 0;
    for (index in list) {
        var item = list[index];
        if (item.id == id) {
            $("#id").val(item.id);
            $("#subtitle").val(item.subtitle);
            $("#url").val(item.url);
            $("#picPath").val(item.picpath);
            $("#picShow").attr("src",item.picpath);
            $("#order_id").val(item.order_id);
            return;
        }
    }
}

function cleanForm() {
    $("#id").val("");
    $("#url").val("");
    $("#subtitle").val("");
    $("#picPath").val("");
    $("#picShow").attr("src", "");
    $("#order_id").val("0");
}
